<template lang="pug">
    el-card(shadow="never",v-if="visible")
      div(slot="header",class="clearfix")
          span 编辑胰岛素品牌
          i(class="el-icon-loading",v-if="querying")

      el-form(:model="newInsulin",ref="newInsulinForm")
        el-form-item(prop="name",:rules="[{required:true, message: '胰岛素泵品牌名称不能为空'}]")
          el-input(placeholder="胰岛素泵品牌名称",v-model="newInsulin.name",type="text",@change="submit",autofocus)

      div(class="card-footer")
          div(class="pull-right")
              el-button(size="small",@click="close")
                span 关闭
              submit-button(@click="submit",type="warning",:loading="submiting")
                i(class="el-icon-check")
                span 保存

</template>

<script>
import axios from "@/plugin/axios";

import InsulinNewModel from "./InsulinNewModel";
import BaseNew from "@/mixins/BaseNew";

export default {
  name: "pump-brand-edit",
  mixins: [BaseNew],
  data() {
    return {
      newInsulin: new InsulinNewModel(),
      querying: false,
    //   visible:false
    };
  },
  watch: {
    id(val) {
      if (val) {
        this.getPumpBrand();
      }
    }
  },
  props: {
    visible: { type: Boolean, default: false },
    id: { type: String, default: null }
  },
  methods: {
    submit() {
      if (this.submiting) return; // 防止重复提交
      this.$refs.newInsulinForm.validate(valid => {
        if (valid) this.doSubmit();
      });
    },
    doSubmit() {
      axios({
        method: "post",
        url: "/PumpBrand/Update",
        data: { model: this.newInsulin }
      })
        .then(data => this.submitSuccess(data))
        .catch(() => (this.submiting = false));
    },
    submitSuccess(data) {
      this.submiting = false;
      this.showUpdateSuccess();
      this.$emit("success"); // 提交事件
    },
    getPumpBrand() {
      this.querying = true;
      axios({
        method: "post",
        url: "PumpBrand/GetPumpBrand",
        data: { id: this.id }
      })
        .then(t => this.getPumpBrandSuccess(t))
        .catch(() => (this.querying = false));
    },
    getPumpBrandSuccess(pumpBrand) {
      this.newInsulin = pumpBrand;
      this.querying = false;
    },
    close() {
      this.visible = false;
      this.$emit("update:visible");
    }
  },
  created() {}
};
</script>
